package com.chookss.home.audit.overtime;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.audit.AuditActivity;
import com.chookss.home.entity.AddworkEntity;
import com.chookss.home.entity.WorkNewEntity;
import com.chookss.tools.MyToast;
import com.chookss.tools.NavigationTools;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.chookss.view.RecycleViewDivider;
import com.google.gson.reflect.TypeToken;
import com.lvgroup.hospital.base.BaseAct;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

public class OvertimeActivity extends BaseAct {
//加班首页
    @BindView(R.id.rvView)
    RecyclerView recyclerView;
    @BindView(R.id.srlView)
    SmartRefreshLayout smrRf;
    @BindView(R.id.ivAudit)
    ImageView ivAudit;
    @BindView(R.id.llNone)
    LinearLayout llNone;
    private OvertimeAdapter adapter;
    private List<AddworkEntity> list = new ArrayList<>();
    private int currentPage = 1;
    private int pageSize = 20;
    private boolean isEnd = false;//是否到底
    private List<AddworkEntity> cacheList = new ArrayList<>();
    private List<AddworkEntity> cacheShopList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_overtime);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();
        NavigationTools.saveUseRecord(this, "1", "41");
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, Utils.dip2px(this, 12), getResources().getColor(R.color.main_bg)));
        adapter = new OvertimeAdapter(R.layout.item_overtime, this, list);
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        recyclerView.setAdapter(adapter);
        smrRf.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (isEnd) {//has next page
                    MyToast.show("没有更多数据了");
                    loadEnd();
                } else {
                    currentPage++;
                    getData();
                }
            }
        });
        smrRf.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                getCache();
                isEnd = false;
                currentPage = 1;
                getData();
            }
        });
        smrRf.autoRefresh();
        getworkList();
    }

    private void getData() {
        Map<String, String> map = new HashMap<>();
        map.put("pageNum", currentPage + "");
        map.put("pageSize", pageSize + "");
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<List<AddworkEntity>>>() {
        }.getType(), null, Urls.selectEmpAddWorkList, SignUtils2.makeSignStr(map), false, new ObserverCallback<List<AddworkEntity>>() {
            @Override
            public void onSuccess(List<AddworkEntity> data) {
                if (currentPage == 1) {
                    list.clear();
                    adapter.notifyDataSetChanged();
                }
                if (smrRf == null) {
                    return;
                }
                if (data != null && data.size() > 0) {
                    if (data.size() < pageSize) {
                        isEnd = true;
                    }
                    if (currentPage == 1 && cacheList.size() > 0) {
                        list.addAll(cacheList);
                        list.addAll(data);
                        adapter.notifyDataSetChanged();
                    } else {
                        int old = list.size();
                        list.addAll(data);
                        adapter.notifyItemRangeChanged(old, data.size());
                    }
                    llNone.setVisibility(View.GONE);
                    smrRf.setVisibility(View.VISIBLE);
                } else {
                    isEnd = true;
                    if (list.size() == 0) {
                        llNone.setVisibility(View.VISIBLE);
                        smrRf.setVisibility(View.GONE);
                    } else {
                        llNone.setVisibility(View.GONE);
                        smrRf.setVisibility(View.VISIBLE);
                    }
                }
                loadEnd();
            }

            @Override
            public void onError(String errorMsg) {
                loadEnd();
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getworkList();
        getCache();
    }
    private void getCache() {
//        cacheList.clear();
//        cacheShopList.clear();
//        cacheShopList.addAll(XJson.getReimbursementDetailEntityCache(this, "shop", ReimbursementDetailEntity.class));
//        for (int i = 0; i < cacheShopList.size(); i++) {
//            if (cacheShopList.get(i).getCacheStatus() == 2) {
//                ShopEntity shopEntity = new ShopEntity();
//                shopEntity.setCacheStatus(2);
//                shopEntity.setCache(cacheShopList.get(i).getCache());
//                shopEntity.setAmt(cacheShopList.get(i).getAmt());
//                shopEntity.setGoodsCode(cacheShopList.get(i).getGoodsCode());
//                shopEntity.setImageUrl(cacheShopList.get(i).getImageUrl());
//                shopEntity.setGoodsName(cacheShopList.get(i).getGoodsName());
//                shopEntity.setGroupName(cacheShopList.get(i).getGroupName());
//                shopEntity.setOwnerEmployeeName(cacheShopList.get(i).getEmployeeName());
//                cacheList.add(shopEntity);
//            }
//        }

    }
    private void getworkList() {
        Map<String, String> map = new HashMap<>();
        //待审核 传0 已审核不传
        map.put("auditStatus", "0");
        map.put("pageNum", "1");
        map.put("pageSize", "1");
        map.put("type", "19");
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<List<WorkNewEntity>>>() {
        }.getType(), null, Urls.selectKqMyWorkList, SignUtils2.makeSignStr(map), false, new ObserverCallback<List<WorkNewEntity>>() {
            @Override
            public void onSuccess(List<WorkNewEntity> data) {
                if (ivAudit == null) {
                    return;
                }
                if (data != null && data.size() > 0) {
                    ivAudit.setVisibility(View.VISIBLE);
                } else {
                    ivAudit.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(String errorMsg) {
                if (ivAudit != null) {
                    ivAudit.setVisibility(View.GONE);
                }
            }
        });
    }

    class OvertimeAdapter extends BaseQuickAdapter<AddworkEntity, BaseViewHolderEx> {
        private Activity context;

        public OvertimeAdapter(int layoutResId, Activity context, @io.reactivex.annotations.Nullable List<AddworkEntity> data) {
            super(layoutResId, data);
            this.context = context;
        }

        @Override
        protected void convert(BaseViewHolderEx helper, AddworkEntity entity) {
            TextView tvTitle = helper.getView(R.id.tvTitle);
            TextView tvStatus = helper.getView(R.id.tvStatus);
            TextView tvTime = helper.getView(R.id.tvTime);
            TextView tvGroup = helper.getView(R.id.tvGroup);
//            tvTitle.setText(Html.fromHtml(title));
            helper.getItemView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Urls.antiShake.check()) {
                        return;
                    }
//                    if (entity.getCacheStatus() == 2) {
//                        //上传失败
//                        for (int i = 0; i < cacheShopList.size(); i++) {
//                            if (cacheShopList.get(i).getCache().equals(entity.getCache())) {
//                                XJson.setReimbursementDetailEntityCache(OvertimeActivity.this, 1, "shop", cacheShopList.get(i));
//                                Intent intent = new Intent(OvertimeActivity.this, ShopAddActivity.class);
//                                intent.putExtra("errorStatus", true);
//                                intent.putExtra("data", cacheShopList.get(i));
//                                startActivity(intent);
//                                smrRf.autoRefresh();
//                            }
//                        }
//                    } else {
//                        Intent intent = new Intent(context, ShopDetailActivity.class);
//                        intent.putExtra("goodsCode", entity.getGoodsCode());
//                        intent.putExtra("type", entity);
//                        if (entity.getStatusCd().equals("7")) {
//                            intent.putExtra("projectDelivery", true);
//                        }
//                        context.startActivity(intent);
//                    }

                }
            });
        }
    }

    private void loadEnd() {
        if (null != smrRf && smrRf.getState() == RefreshState.Refreshing)
            smrRf.finishRefresh(true);
        if (null != smrRf && smrRf.getState() == RefreshState.Loading)
            smrRf.finishLoadMore(true);
    }

    @OnClick({R.id.common_title_back,  R.id.ll_audit})
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.ll_audit:
                intent = new Intent(this, AuditActivity.class);
                intent.putExtra("type", 19);
                startActivity(intent);
                break;
        }
    }
}
